package com.kw.drug.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kw.drug.common.ResultMapUtil;
import com.kw.drug.entity.Druginfo;
import com.kw.drug.entity.Returngoods;
import com.kw.drug.entity.Supplier;
import com.kw.drug.service.SaleinfoService;
import com.kw.drug.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    SupplierService supplierService;

    @RequestMapping("/supplierList")
    @ResponseBody
    public Map supplierList(String param, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        try {
            Page<Supplier> supplierPage = supplierService.selectForsupplierList(pageNum, pageSize, param);
            return ResultMapUtil.getHashMapMysqlPage(supplierPage);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping()
    public String supplierIndex() {
        return "supplier";
    }
    /*
    分页返回数据
     */


    @RequestMapping("/supplierQueryPage")
    @ResponseBody
    public Map supplierQueryPage(String param, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        try {
            IPage<Supplier> ipage = supplierService.selectForsupplierList(pageNum, pageSize, param);
            return ResultMapUtil.getHashMapMysqlPage(ipage);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 进入编辑页面
     */
    @RequestMapping("/supplierQueryById")
    public String editIndex(Integer id, Model model) {
        Supplier supplier = supplierService.selectSupplierById(id);
        model.addAttribute("obj", supplier);
        return "supplierPage";
    }

    /**
     * 编辑后提交
     */
    @RequestMapping("/supplierEdit")
    @ResponseBody
    public Map supplierEdit(Supplier supplier) {
        try {
            Integer integer = supplierService.updateSupplier(supplier);
            return ResultMapUtil.getHashMapSave(integer);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 进入添加页面
     */
    @RequestMapping("/supplierPage")
    public String addIndex() {
        return "supplierPage";
    }

    /**
     * 添加供应商
     */
    @RequestMapping("/supplierAdd")
    @ResponseBody
    public Map addSupplier(Supplier supplier) {
        try {
            Integer integer = supplierService.addSupplier(supplier);
            return ResultMapUtil.getHashMapSave(integer);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }
}



